package servlets;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.BadLocationException;
import javax.swing.text.ElementIterator;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;

import pos.domain.Event;
import pos.domain.EventImpl;

/**
 * Servlet implementation class ParseProcess
 */
public class ServletParseProcess extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletParseProcess() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO El gran zurullo
		// HttpSession session = request.getSession();

		/**
		 * Extract all "posts" tags from an HTML document.
		 */
		System.out.println("llega Servlet");
		String surl = request.getParameter("url");
		System.out.println(surl);

		/*
		 * Distinctions between sections
		 */
		List<String> res = new LinkedList<String>();
		if (surl.contains("cinema")) { // if url contains name section

			res = this.parseMethod(surl);

		} else if (surl.contains("litterature")) {
			res = parseMethod(surl);
		} else if (surl.contains("expos")) {
			res = parseMethod(surl);
		} else if (surl.contains("scenes")) {
			res = parseMethod(surl);
		} else if (surl.contains("musique")) {
			res = parseMethod(surl);
		} else if (surl.contains("concours")) {
			res = parseMethod(surl);
		}

		System.out.println(res);
	}

	
	public static String html2text(String html) {
	    return Jsoup.parse(html).text();
	}

	/*
	 * Méthod for parse a URL of any section
	 */
	public List<String> parseMethod(String s) throws IOException {

		// List<String> listID = new LinkedList<String>();
		List<String> listHref = new LinkedList<String>();
		
		/**
		 *  Open URL with HTMLEditorKit (Java Swing library).
		 */
		
		URL url = new URL(s);

		HTMLEditorKit kit = new HTMLEditorKit();
		HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
		doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
		Reader HTMLReader = null;
		try {
			HTMLReader = new InputStreamReader(url.openConnection()
					.getInputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			kit.read(HTMLReader, doc, 0);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}

		// Get an iterator for all HTML tags.
//		ElementIterator it = new ElementIterator(doc);
//		Element elem;

	//	while ((elem = it.next()) != null) {
			
			listHref = retrieveLinks(s,doc);
			List<Event> listEvent = new LinkedList<Event>();
			
			/** 
			 * next page of section recursively
			 */
			 for(String aux: listHref){
				// pattern to check if string is for go to next page
				Pattern p1 = Pattern.compile("http://www.rtbf.be/culture/cinema/page/\\.*?/");
				Matcher m1 = p1.matcher(aux);
				if(!m1.find()){
					Event e = null;
						if(!listEvent.contains(e)){
							e = this.createEvent(aux); // create event of post page
							if(e==null)
								throw new NullPointerException("Error al crear evento");
							listEvent.add(e);
							System.out.println("Evento creado");
						}
					// TODO código para meter el evento en la BD.
					
				}
				else {
					System.out.println("link a pagina "+ aux);
					this.parseMethod(aux); // call method if link is for go to next page
				}
				
			}
			

		/*	 if (elem.getName().equals("div")) {
				String sAtt = (String) elem.getAttributes().getAttribute(
						HTML.Attribute.ID);
				// Regular expression for match posts with "post-xxxxx"

				try {
					Pattern p1 = Pattern.compile("post-\\d{5}");
					Matcher m1 = p1.matcher(sAtt);
					if (m1.find()) { // if match
						if (!listID.contains(sAtt)) // if post is not included yet
							listID.add(sAtt);
					}
					
					// System.out.println(sAtt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			
			if (elem.getName().equals("div")){
				System.out.println("yes div");
				String sHref = null;
				
				// Regular expression for match all url of posts.
				
				try{
					Pattern p2 = Pattern.compile("http://www.rtbf.be/culture/cinema/tintin-en-tete-du-box-office-en-chine/");
					Matcher m2 = p2.matcher(sHref);
					if(m2.find()){
						System.out.println(sHref);
						if(!listHref.contains(sHref))
							listHref.add(sHref);
					}
				} catch (Exception e){
					e.printStackTrace();
				}
			}

		}
		*/
	//	}
		System.out.println("llega parseMethod");
		return listHref;
	}
	
	/**
	 * This method is for to obtain all links of a URL given as parameter
	 * 
	 * @param s URL which links are obtained
	 * @param d object HTMLDocument
	 * @return List<String> with all links of 's'
	 */
	public List<String> retrieveLinks (String s, HTMLDocument d){
		
			List<String> listLinks = new LinkedList<String>();
			
//			HTMLEditorKit kit = new HTMLEditorKit();
//			HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
//			doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
//			Reader HTMLReader = null;
//			URL url = new URL(s);
			
			/**
			 *  Open URL with HTMLEditorKit (Java Swing library).
			 */
			
			try{
//				HTMLReader = new InputStreamReader(url.openConnection().getInputStream());
//				try {
//					kit.read(HTMLReader, doc, 0);
//				} catch (BadLocationException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				
				// Finding "a" elements of HTML
				HTMLDocument.Iterator it = d.getIterator(HTML.Tag.A);
				while(it.isValid()){
					SimpleAttributeSet sa = (SimpleAttributeSet)it.getAttributes();
					String link = (String)sa.getAttribute(HTML.Attribute.HREF);
					Pattern p = Pattern.compile("http://www.rtbf.be/culture/cinema/\\.*");
					Matcher m = p.matcher(link);
					// System.out.print(m.find());
					
					if(m.find()){
						if(!link.contains("#comment")){ // for a rare href that is true in Pattern.
							if(!listLinks.contains(link)){
								listLinks.add(link);
							}
						}
						
						
					}
					it.next(); // inc iterator
					
					
				}
				
		} catch(Exception e){
			e.printStackTrace();
		}
			return listLinks;
	}
	
	public Event createEvent (String s) throws MalformedURLException, IOException{
	
		/**
		 *  Open URL with HTMLEditorKit (Java Swing library).
		 */
		URL url = new URL(s);

		HTMLEditorKit kit = new HTMLEditorKit();
		HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
		doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
		Reader HTMLReader = null;
		try {
			HTMLReader = new InputStreamReader(url.openConnection()
					.getInputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			kit.read(HTMLReader, doc, 0);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		
		// Parsing info
		
		ElementIterator it = new ElementIterator(doc);
		javax.swing.text.Element elem=null;
		Event e= new EventImpl();
		
		while((elem=it.next())!=null){
				// TODO sacar los meta description y meta keywords
			String attName = (String) elem.getAttributes().getAttribute(HTML.Attribute.NAME);
			String sAtt  = (String) elem.getAttributes().getAttribute(HTML.Attribute.CONTENT);

			if(elem.getName().equals("meta")){ // if tag is "meta"
				if(attName!=null){				
					if(attName.equals("description")){
						// e.setDescriptionEvent(sAtt);
						System.out.println("Description of meta tag: "+sAtt); 
					}
				}
			}
	
		}
		
		/**
		 * Uso de Jsoup para extraer texto de Tags, muy posiblemente la herramienta a usar!!!!
		 */

		Document d = Jsoup.connect(s).get();
		String title = d.title();
		// Elements y Element de Jsoup
		Elements description = d.getElementsByTag("strong");
		for(Element el: description){
			String desc = el.text();
			System.out.println("description of strong: "+ desc);
			e.setDescriptionEvent(desc);
		}
		
		return e;
		
	}
}